package com.kaishengit.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaishengit.entity.Project;
import com.kaishengit.entity.User;
import com.kaishengit.service.ProjectService;
import com.kaishengit.service.UserService;
import com.kaishengit.util.web.ServletUtil;

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		UserService userService = new UserService();
		ProjectService projectService = new ProjectService();
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		User user = userService.login(username, password);
		List<User> allUser = userService.findAllUser();
		ServletUtil.setSessionAttirbute(request, "allUser", allUser);
		if(user != null) {
			ServletUtil.setSessionAttirbute(request,"curr_user",user);
			
			List<Project> list = projectService.findProjectByUser(user);
			ServletUtil.setSessionAttirbute(request, "projectList", list);
			ServletUtil.redirect(response, "main.jspx");
			
		} else {
			ServletUtil.setRequestAttribute("state", "10001", request);
			ServletUtil.forword(request, response, "login");
		}
	}
	
}
